# coding=utf-8

# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render, redirect

from dockerManagerApp import models
from dockerManagerApp.model import user_validate, host_manager, images_manager, store_manager, container_manager
from dockerManagerApp.model.base import upload_file
from dockerManagerApp.model.base.rpc_client import rpcClientFunction


def empty(request):
    if request.method == 'GET':
        print("get.... ")
        return render(request, 'testDaemon.html')
        # return HttpResponse("docker 管理台")
    else:

        print(request.POST.get('name_input_1'))
        print(request.POST.get('name_input_2'))
        print(request.POST.get('name_input_3'))
        print(request.POST.get('name_input_4'))
        print(request.POST.get('name_input_5'))
        print(request.POST.get('name_input_6'))

        return HttpResponse("docker ... ")


def login(request):
    '''用户登录认证'''
    if request.method == 'POST':
        return user_validate.user_validate(request)
    return render(request, 'login.html')


def homeindex(request):
    '''显示主页面'''
    session_username = request.session.get('django_username')
    if str(session_username) == "None":
        return redirect('/login')
    request.session['session_username'] = session_username
    return render(request, 'home.html', {"username": session_username})


def userLogout(request):
    '''用户注销'''
    return user_validate.user_logout(request)


def hostManager(request):
    return host_manager.get_all_host(request)


def hostList(request):
    return host_manager.get_all_host(request, retuType=1)


def hostAction(request):
    return host_manager.action_host(request)


def nodesInfo(request, nid):
    return host_manager.item_host_info(request, nid)


def imagesManager(request):
    return images_manager.get_all_images(request)


def storeManager(request):
    return store_manager.get_all_store(request)


def uploadFile(request):
    return upload_file.upload_file(request)


def downloadFile(request, filename):
    return upload_file.file_down(request, filename)


def importImage(request):
    return host_manager.imageManager(request)


def containerManager(request):
    return container_manager.containerManagerFun(request)


def container_Manager(request):
    return container_manager.container_ManagerFun(request)


def updateNodeStatus():
    '''timer'''
    try:
        host_manager.update_node_status_fun()
    except Exception as e:
        print('Exception', e.message)
